package com.bwstudio.demo.consumer.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import com.bwstudio.demo.consumer.service.DeferredResultQueue;
import com.bwstudio.demo.consumer.service.DeferredResultService;
import com.bwstudio.demo.consumer.ui.dto.DeferredResultResponse;

@RestController
@RequestMapping(value = "/deferred-result")
public class DeferredResultController {

	@Autowired
	private DeferredResultService deferredResultService;

	// ------------------- 模拟异步获取结果场景  -----------------------------------------
	
	/**
	 * 为了方便测试，简单模拟一个 多个请求用同一个requestId会出问题
	 */
	private final String requestId = "haha";

	@GetMapping(value = "/get")
	public DeferredResult<DeferredResultResponse> get(
			@RequestParam(required = false, defaultValue = "10000") Long timeout) {
		DeferredResult<DeferredResultResponse> deferredResult = new DeferredResult<>(timeout);

		deferredResultService.process(requestId, deferredResult);

		return deferredResult;
	}

	/**
	 * 设置DeferredResult对象的result属性，模拟异步操作
	 * 
	 * @param desired
	 * @return
	 */
	@GetMapping(value = "/result")
	public String settingResult(
			@RequestParam(value = "desired", required = false, defaultValue = "成功") String desired) {
		DeferredResultResponse deferredResultResponse = new DeferredResultResponse();
		if (DeferredResultResponse.Msg.SUCCESS.getDesc().equals(desired)) {
			deferredResultResponse.setCode(HttpStatus.OK.value());
			deferredResultResponse.setMsg(desired);
		} else {
			deferredResultResponse.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
			deferredResultResponse.setMsg(DeferredResultResponse.Msg.FAILED.getDesc());
		}
		deferredResultService.settingResult(requestId, deferredResultResponse);

		return "Done";
	}
	
	// ------------------- 模拟异步创建订单创建 -----------------------------------------
	
	 /**
     * 异步创建订单
     * 只返回DeferredResult，并不真正创建订单
     * @return
     */
	@GetMapping("/createOrder")
    public DeferredResult<String> createOrder() {
        //新建DeferredResult实例，并保存到队列中，参数的意义是最长等待5秒，否则返回值自动设置为timeout
        DeferredResult<String> deferredResult = new DeferredResult<>((long)5000, "timeout");
        DeferredResultQueue.save(deferredResult);
        //返回
        return deferredResult;
    }

    /**
     * 真正的创建订单
     * @return
     */
    @GetMapping("/create")
    public String createOrderActioin() {
        //随机生成订单号
        String orderNum = UUID.randomUUID().toString();
        //从队列中取出DeferredResult
        DeferredResult<String> deferredResult = DeferredResultQueue.get();
        //向DeferredResult中存值
        deferredResult.setResult(orderNum);
        //返回
        return orderNum;
    }
}
